package com.base.mail;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.mail.adapter.BaseAdapter;
import com.base.mail.adapter.BaseHolder;
import com.base.mail.adapter.ContactAdapter;
import com.base.mail.bean.ContactEntity;
import com.base.mail.databinding.FragmentPersonBinding;
import com.base.mail.presenter.PresonPresenter;
import com.base.mail.utils.LetterView;
import com.base.mail.view.PresonView;

import java.util.List;

/**
 * Created by weikailiang on 2019/11/20.
 */

public class PresonFragment extends CBaseMvpFragment<PresonPresenter> implements PresonView {
    private FragmentPersonBinding mBinding;
    private LinearLayoutManager mLinearLayoutManager;
    private ContactAdapter mAdapter;
    private Runnable mOverlay_gone;
    private Handler mHandler=new Handler();
    @Override
    public void init() {
        mBinding = (FragmentPersonBinding) mRootBinding;
        mPresenter = new PresonPresenter(this,_mActivity);
    }

    @Override
    public void start() {
        mBinding.llReload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //树状机构
                mPresenter.getContacts();
            }
        });


        mPresenter.getContacts();
        mOverlay_gone = new Runnable() {

            @Override
            public void run() {
                mBinding.tvOverlay.setVisibility(View.GONE);
            }
        };
        mBinding.letter.setOnLetterSelectedListener(new LetterView.LetterSelectedListener() {
            @Override
            public void onLetterSelectedListener(String letter) {
                if(mAdapter!=null&&mAdapter.mLetterIndexer!=null)
                {
                    int section = mAdapter.mLetterIndexer.getSectionForItem(letter);
                    if(section>=0)
                    {
                        int position = mAdapter.mLetterIndexer.getPositionForSection(section);
                        if(position>=0)
                        {
                            int firstP = mLinearLayoutManager.findFirstVisibleItemPosition();
                            int lastP = mLinearLayoutManager.findLastVisibleItemPosition();
                            if(position>=firstP&&position<=lastP)
                            {
                                View childView = mBinding.rv.getChildAt(position - firstP);
                                mBinding.rv.scrollBy(0,childView.getTop());
                            }else
                            {
                                mLinearLayoutManager.scrollToPosition(position);
                            }

                        }
                    }
                }

                mHandler.removeCallbacks(mOverlay_gone);
                mBinding.tvOverlay.setVisibility(View.VISIBLE);
                mBinding.tvOverlay.setText(letter);
                mHandler.postDelayed(mOverlay_gone,1500);
            }
        });
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_person;
    }

    @Override
    public void setData(List<ContactEntity> data) {
        mLinearLayoutManager = new LinearLayoutManager(mContext);
        mAdapter = new ContactAdapter(data, mContext);
        mAdapter.putNormalType(R.layout.item_contact);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                ARouter.getInstance().build("/mail/maildetail")
                        .withString("userId",mPresenter.mData.get(dataPosition).getUserid()).navigation();
            }
        });
        mBinding.rv.setLayoutManager(mLinearLayoutManager);
        mBinding.rv.setAdapter(mAdapter);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getView() != null) {
            getView().setVisibility(isVisibleToUser ? View.VISIBLE : View.INVISIBLE);
        }
    }
    @Override
    public void showReload() {
        mBinding.llReload.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideReload() {
        mBinding.llReload.setVisibility(View.GONE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
